package com.ace.lab.base.util;


import com.ace.lab.base.model.Result;
import com.google.protobuf.Descriptors;
import com.google.protobuf.GeneratedMessageV3;

import java.util.HashMap;
import java.util.Map;

public class ResultBuilder {

    private final Result result = new Result();

    public ResultBuilder msg(String message) {
        result.setMessage(message);
        return this;
    }

    public ResultBuilder code(String respCode) {
        result.setRespCode(respCode);
        return this;
    }

    public ResultBuilder data(Object data) {
        if (data instanceof GeneratedMessageV3) {
            Map<String, Object> map = new HashMap<>();
            Map<Descriptors.FieldDescriptor, Object> fields = ((GeneratedMessageV3) data).getAllFields();
            fields.forEach((k, v) -> {
                String name = k.getName();
                map.put(name, v);
            });
            data = map;
        }
        result.setData(data);
        return this;
    }

    public Result build() {
        return result;
    }
}
